<template>
    <rx-layout style="padding: 10px;">
        <template #middle>
            <rx-fit v-model="fitSearch">
                <template #head>
                  <div style="display: flex;align-items: center;justify-content: space-between;">
                    <rx-list-title :title="i18n('list.asyncDown','异步下载')"></rx-list-title>
                    <div class="table-operator">
                      <a-button type="danger" @click="delByIds(selectedRowKeys)" v-show="selectedRowKeys && selectedRowKeys.length>0">
                        <template #icon>
                          <delete-outlined/>
                        </template>
                        {{$ti18('rxList.delBtn','删除')}}
                      </a-button>
                    </div>
                    <span class="search-btn" @click.stop="searchshow" @mousedown.stop><SearchOutlined/>{{ $ti18('rxList.leachBtn', '过滤') }}</span>
                  </div>
                </template>
                <template #search>
                  <a-form layout="vertical">
                    <a-form-item :label="i18n('form.fileName','文件名称')">
                      <a-input :placeholder="i18n('form.fileName','文件名称','input')" v-model:value="urlConfig.queryParam.Q_FILE_NAME__S_LK"/>
                    </a-form-item>
                  </a-form>
                  <a-form layout="vertical">
                    <a-form-item :label="i18n('form.ownerName','所属列表')">
                      <a-input :placeholder="i18n('form.ownerName','请输入所属列表','input')" v-model:value="urlConfig.Q_LIST_NAME__S_LK"/>
                    </a-form-item>
                  </a-form>
                  <div class="list-search-toolbar">
                    <a-button type="primary" @click="search">
                      <SearchOutlined/>
                      {{ $ti18('rxList.searchBtn', '查询') }}
                    </a-button>
                    <a-button style="margin-left: 4px" @click="resetSearch">
                      <RedoOutlined />
                      {{ $ti18('rxList.resetBtn', '重置') }}
                    </a-button>
                  </div>
                </template>
                <template #middle>
                  <rx-grid ref="table"
                           :hasPage="true"
                           :urlConfig="urlConfig"
                           :selectConfig="selectConfig"
                           url="/api-form/form/core/formExcelGenTask/query"
                           field="id"
                           :columns="columns"
                  >
                    <template #remark="{row}">
                      {{getRemark(row)}}
                    </template>
                    <template #fileName="{row}">
                      <a @click.stop="downloadExcel(row)">{{row.fileName}}</a>
                    </template>
                    <template #genStatus="{row}">
                      <a-tag :color="statusMap[row.genStatus].color">
                        <span>{{ statusMap[row.genStatus].text }}</span>
                      </a-tag>
                    </template>
                    <template #action="{ row }">
                      <RxGridAction :size="2">
                        <div v-show="row.fileId" @click.stop="downloadExcel(row)">{{$ti18('rxList.download','下载')}}</div>
                        <div v-show="row.fileId" @click.stop="getDownloadRecord(row)">{{i18n('list.record','下载记录')}}</div>
                        <div @click.stop="delByIds([row.id])">{{$ti18('rxList.delBtn','删除')}}</div>
                      </RxGridAction>
                    </template>
                  </rx-grid>
                </template>
            </rx-fit>
            <a-drawer
                :title="i18n('list.record','下载记录')"
                placement="right"
                :closable="true"
                :open="visible"
                @close="onClose"
                :maskClosable="true"
                :destroyOnClose="true"
                width="600"
            >
               <FormDownloadRecordList :selectGenTaskId="selectGenTaskId" ></FormDownloadRecordList>
            </a-drawer>
        </template>
    </rx-layout>
</template>

<script>
import FormExcelGenTaskApi from '@/common/api/form/formExcelGenTask'
import FormExcelGenTaskEdit from './FormExcelGenTaskEdit.vue'
import FormDownloadRecordList from './FormDownloadRecordList.vue'
import {BaseList, RxFit, RxGrid,TokenUtil} from '@lowcode/jpaas-base-lib';

export default {
    name: 'FormExcelGenTaskList',
    mixins: [BaseList],
    components: {
        RxFit,
        FormDownloadRecordList
    },
    data() {
        return {
            urlConfig: {
              ajaxType: 'post',
              contentType: 'json',
              queryParam: {},
              dataField: 'result.data',
              totalField: 'result.totalCount'
            },
            selectConfig: {
              type: 'checkbox',
            },
            columns: [
                {title: this.i18n('form.fileName','文件名称'), field: 'fileName', minWidth: 100, slots: {default: 'fileName'}},
                {title: this.i18n('form.owner','所属人'), field: 'createByName', minWidth: 100},
                {title: this.i18n('form.createTime','生成时间'), field: 'createTime', minWidth: 100},
                {title: this.i18n('form.status','状态'), field: 'genStatus', minWidth: 100, slots: {default: 'genStatus'}},
                {title: this.i18n('form.ownerName','所属列表'), field: 'listName', minWidth: 100},
                {title: this.i18n('form.remarks','备注'), field: 'remark', minWidth: 100, slots: {default: 'remark'}},
                {title: this.$ti18('rxList.action','操作'), minWidth: 120, field: 'action', slots: {default: 'action'}}
            ],
            component: FormExcelGenTaskEdit,
            comment: this.i18n('list.comment','Excel生成任务'),
            widthHeight: ['800px', '600px'],
            visible:false,
            //选择的任务ID
            selectGenTaskId:"",
            statusMap: {
                0: {color: 'red', text: this.i18n('statusMap.failure','生成失败')},
                1: {color: 'green', text: this.i18n('statusMap.success','生成成功')},
                2: {color: 'blue', text: this.i18n('statusMap.generation','生成中')},
                25: {color: 'blue', text: this.i18n('statusMap.generation1','生成中25%')},
                50: {color: 'blue', text: this.i18n('statusMap.generation2','生成中50%')},
                75: {color: 'blue', text: this.i18n('statusMap.generation3','生成中75%')},
                100: {color: 'green', text: this.i18n('statusMap.success','生成成功')},
            },
        }
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"FormExcelGenTask",key);
        },
        getRemark(record){
            if(record.remark){
                return record.remark;
            }
            return "";
        },
        delById(ids) {
            return FormExcelGenTaskApi.del({ids: ids.join(',')});
        },
        downloadExcel(record){
            var accessToken = TokenUtil.getToken();
            var url=`/api/api-system/system/core/sysFile/download/${record.fileId}?accessToken=`+ accessToken;
            window.open(url);
            FormExcelGenTaskApi.downloadRecord({genTaskId:record.id});
        },
        getDownloadRecord(record){
            this.selectGenTaskId="";
            this.visible=true;
            this.selectGenTaskId=record.id;
        },
        onClose(){
            this.visible=false;
        }

    }
}
</script>
<style scoped>

</style>

